@extends('wap.layout')
@section('content')
	<!--搜索框-->
	<div class="weui-search-bar" id="searchBar">
	  <form class="weui-search-bar__form" action="/user_file/search">
	    <div class="weui-search-bar__box">
	      <i class="weui-icon-search"></i>
	      <input type="search" class="weui-search-bar__input" name="search_name" id="searchInput" placeholder="搜索" required="">
			<input type="hidden" name="folder_id" id="folder_id" />
			<input type="hidden" name="is_public" id="is_public" />
	      <a href="javascript:" class="weui-icon-clear" id="searchClear"></a>
	    </div>
	    <label class="weui-search-bar__label" id="searchText">
	      <i class="weui-icon-search"></i>
	      <span>搜索</span>
	    </label>
	  </form>
	  <a href="javascript:" class="weui-search-bar__cancel-btn" id="searchCancel">取消</a>
	</div>
	<!--搜索框end-->
	<!--返回-->
	<div class="gobackBar">
		<a href="/index">返回上一级</a>
		<a class="orderby" id='orderby' href="javascript:void(0);">排序</a>
	</div>
	<!--返回end-->
	<!--文件列表-->
	<div class="fileList">
		<div class="weui-panel weui-panel_access">
		    <!-- body 部分 -->
		    <ul class="weui-panel__bd">

		    </ul>
		</div>
	</div>
	<!--文件列表end-->
	<div class="pldIcon"><i></i></div>
	<div class="pInfoBar">暂无文件</div>
	<!--如有文件，则显示-->
	{{--<div class="pInfoBar">298389个文件夹，49857643个文件</div>--}}
	<!--如有文件，则显示end-->



<div id="full" class='weui-popup__container'>
	<div class="weui-popup__overlay"></div>
	<div class="weui-toptips bg-warning weui-toptips_visible upTips">正在上传第10/99个文件...</div>
	<div class="weui-popup__modal">
		<div class=""></div>
		<ul class="weui-panel__bd">
			<div style="height: 30px;" id="show_pop_filelist"></div>

		</ul>
		<div style="height: 40px;"></div>
	</div>
	<a href="javascript:;" class="weui-btn weui-btn_primary close-popup">关闭</a>
</div>
<a href="javascript:;" class="weui-btn weui-btn_primary open-popup" data-target="#full" style="display: none" id="show_pop">显示全屏(默认)Popup</a>


	<div class="placeHolderBar"></div>
	<!--上传-->
	<div class="uploadBtnList">
		<a href="javascript:;" id='show-actions'><i></i></a>
	</div>
	<!--上传end-->
<style>
.weui-dialog__bd {font-size:18px}
</style>
<input type="file" multiple="multiple" id="inputfile" style="display:none" />
<a href="javascript:;" id='show-custom' class="weui-btn weui-btn_primary" style="display:none;">显示自定义对话框</a>
<div class="mask"></div>
<div class="weui-actionsheet  weui-actionsheet_toggle orderOpt" id="oderOpt">
	<div class="weui-actionsheet__title">排序方式</div>
	<div class="weui-actionsheet__menu">
		<div class="weui-actionsheet__cell color-primary" id="order_file_name" data-key="1">按名称<span>降序</span></div>
		<div class="weui-actionsheet__cell" id="order_file_size" data-key="2">按大小<span>降序</span></div>
		<div class="weui-actionsheet__cell" id="order_file_time" data-key="3">按时间<span>降序</span></div>
	</div>
	<div class="weui-actionsheet__action">
		<div class="weui-actionsheet__cell weui-actionsheet_cancel odercancel">取消</div>
	</div>
</div>
<input type="hidden" name="key" value="1">
<input type="hidden" name="order" value="0">
@stop

@section('ownJs')
<script src="/js/sparkmd5.min.js"></script>
<script src="/js/upload.js"></script>
<script>
	var folder_id = getParameterByName('folder_id');
	var is_public = parseInt(getParameterByName('is_public'));
	var download_links,upload_time = 0,files,error_files=[],success_files_number=0,failed_files_number=0;
$(function() {
	$('#folder_id').val(folder_id);$('#is_public').val(is_public);
	getFile();
	getParentId();
	var mask = $(".mask");
	orderOpt = $(".orderOpt");
	odercancel= $(".odercancel");
	$("#orderby").click(function(){
		mask.show();
		orderOpt.slideToggle('fast')
	});
	mask.click(function(){
		$(this).hide();
		orderOpt.slideToggle('fast')
	});
	odercancel.click(function(){
		mask.hide();
		orderOpt.slideToggle('fast')
	});
	$('#order_file_name, #order_file_size, #order_file_time').click(function(){
		var key = $('input[name="key"]').val();
		if(key != $(this).data('key')){
			$('input[name="key"]').val($(this).data('key'));
			$('input[name="order"]').val(0);
			$(this).addClass('color-primary');
			$(this).siblings().removeClass('color-primary').find('span').text('降序')
		}else{
			if($('input[name="order"]').val() == '0'){
				$('input[name="order"]').val(1);
				$(this).find('span').text('升序');
			}else{
				$('input[name="order"]').val(0);
				$(this).find('span').text('降序');
			}
		}
		getFile();
		mask.click();
	});
	//上传菜单
	$(document).on("click", "#show-actions", function() {
	    $.actions({
	      onClose: function() {
	      },
	      actions: [
	        {
	          text: "新建文件夹",
	          onClick: function() {
	        	location = '/create?folder_id='+folder_id+'&is_public='+is_public;
	          }
	        },
	        {
	          text: "上传",
	          onClick: function() {
				  F_Open_dialog();
	          }
	        }
	      ]
	    });
	});
	//响应文件添加成功事件
	$("#inputfile").change(function(){
		files = document.getElementById('inputfile').files;
		$('#show_pop').trigger('click');
		showUploadProgress();
	});
	$('#full .close-popup').click(function () {
		location.href=location.href;
	})
	//文件操作
	$(".fileList, #full").on("click","i.ops",function() {
		$(this).parent("li.listItem").find("i.ops").toggleClass("open");
		$(this).parent("li.listItem").siblings().find(".ops").removeClass("open");
		$(this).parent("li.listItem").siblings().find(".opList").addClass("dis");
		$(this).parent("li.listItem").find(".opList").toggleClass("dis");
	});
	//重新上传
	$(document).on("click", ".iReupload", function() {
		var file_number = $(this).data('file_number');
		var upload_time = $(this).data('upload_time');
		$('#'+upload_time+'upload'+file_number).remove();
		failed_files_number--;
		files = [];
		files.push(error_files[upload_time][file_number]);
		showUploadProgress();
	});
	//清除
	$(document).on("click", ".iClear", function() {
		var file_number = $(this).data('file_number');
		var upload_time = $(this).data('upload_time');
		$('#'+upload_time+'upload'+file_number).remove();
		failed_files_number--;
	});
	//文件菜单操作
	$(document).on("click", ".iFav", function() {
		var id = $(this).data('id');
		if($(this).hasClass("ck")){
			cancelCollectFile(id);
		}else{
			collectFile(id);
		}
	});

	//删除弹框
	$(document).on("click", ".iDel", function() {
		var id = $(this).data('id');
        $.confirm("您确定要删除文件吗?", "确认删除?", function() {
			deleteFile(id);
        }, function() {
          //取消操作
        });
    });

	//下载文件
	$(document).on("click", ".iDownload", function() {
		var id = $(this).data('id');
		download(id);
	});
	$(document).on("click", "#show-custom", function() {
		$.modal({
			title: '',
			text: download_links,
			buttons: [
				{ text: "复制该链接到浏览器中下载", className: "default"},
//				{ text: "确定", onClick: function(){ $.alert("确定"); } },
			]
		});
	});

	//重命名文件或文件夹
	$(document).on("click", ".iEdit", function() {
		var id = $(this).data('id');
		$.prompt({
			title: '重命名',
//			text: '内容文案',
			input: '',
			empty: false, // 是否允许为空
			onOK: function (input) {
				rename(id, input);
			},
			onCancel: function () {
				//点击取消
			}
		});
	});

});
	function F_Open_dialog(){
		document.getElementById("inputfile").click();
	}
	function showUploadProgress(){
		$('.uploadBtnList').hide();
		upload_time++;
		error_files[upload_time] = [];
		$('#full .upTips').html("共"+files.length+"个文件,上传中");
		for( var j = 0 ; j < files.length ; j++){
			upload(folder_id, files[j], j, (c)=>{uploadSuccess(c)}, (error_type,number)=>{uploadFailed(error_type,number);}, (loaded, total, speed)=>{console.log("uploaded:"+loaded+"/"+total+"@"+speed+"B/s")});
		}
	}

	function uploadSuccess(number){
		success_files_number++;
		var pos=files[number].name.lastIndexOf(".");
		var uploadHtml = '';
		uploadHtml += '<li  class="listItem" id="'+upload_time+'upload'+number+'">'
				+'<a href="javascript:void(0)" class="weui-media-box weui-media-box_appmsg">'
				+'<div class="weui-media-box__hd">'
				+'<i class="fileIcon '+getIconClass(files[number].name.substring(pos).toUpperCase())+'"></i>'
				+'</div>'
				+'<div class="weui-media-box__bd">'
				+'<h4 class="weui-media-box__title">'+ files[number].name.substring(0,pos)+'</h4>'
				+'<p class="weui-media-box__desc">'+currentTime()+'</p>'
				+'</div>'
				+'</a>'
				+'</li>';
		$('#full #show_pop_filelist').after(uploadHtml);
		if(number+1 == files.length){
			$('#full .upTips').html("上传完成,成功:"+success_files_number+"个,失败:"+failed_files_number+"个");
		}
		console.log("upload done:"+number);$('#'+upload_time+'upload'+number).prepend('<em class="tag success">上传成功</em>');
	}

	function uploadFailed(error_type, number){
		failed_files_number++;console.log(failed_files_number);
		error_files[upload_time][number] = files[number];
		console.log(error_files)
		var pos=files[number].name.lastIndexOf(".");
		var uploadHtml = '';
		uploadHtml += '<li  class="listItem" id="'+upload_time+'upload'+number+'">'
				+'<i class="ops"><b></b></i>'
				+'<a href="javascript:void(0)" class="weui-media-box weui-media-box_appmsg filetype">'
				+'<div class="weui-media-box__hd">'
				+'<i class="fileIcon '+getIconClass(files[number].name.substring(pos).toUpperCase())+'"></i>'
				+'</div>'
				+'<div class="weui-media-box__bd">'
				+'<h4 class="weui-media-box__title">'+ files[number].name.substring(0,pos)+'</h4>'
				+'<p class="weui-media-box__desc">'+currentTime()+'</p>'
				+'</div>'
				+'</a>'
				+'<div class="opList dis">'
				+'<a href="javascript:;" class="iReupload" id="iReupload'+ number+'" data-file_number="'+ number+'" data-upload_time="'+upload_time+'">'
				+'<div class="btnIcon"><i></i></div>'
				+'<p class="btnLabel">重新上传</p>'
				+'</a>'
				+'<a href="javascript:;" class="iClear" id="iClear'+ number+'" data-file_number="'+ number+'" data-upload_time="'+upload_time+'">'
				+'<div class="btnIcon"><i></i></div>'
				+'<p class="btnLabel">清除</p>'
				+'</a>'
				+'</div>'
				+'</li>';
		$('#full #show_pop_filelist').after(uploadHtml);
		if(number+1 == files.length){
			$('#full .upTips').html("上传完成,成功:"+success_files_number+"个,失败:"+failed_files_number+"个");
		}
		console.log("upload failed:"+error_type);$('#'+upload_time+'upload'+number).prepend('<em class="tag fail">'+error_type+'</em>');
	}
	function getParentId(){
		$.ajax({
			type: 'get',
			url: ' /user_file/parent/'+folder_id,
			data:{is_public:is_public},
			dataType: 'json',
			success: function (result) {
				if (result.code == 0) {
					if(result.data.parent_id > 0) {
						$('.gobackBar a:first-child').attr('href', '/list?folder_id=' + result.data.parent_id+'&is_public='+is_public);
					}
				}else if(result.code == -1){
					location.href = '/error';
				}
			}
		});
	}
	//收藏文件
	function collectFile(id){
		$.ajax({
			type: 'get',
			url: ' /collect_file/collect/'+id,
			dataType: 'json',
			success: function (result) {
				if (result.code == 0) {
					$('#iFav'+id).toggleClass("ck");
				}else if(result.code == -1){
					location.href = '/error';
				}
			}
		});
	}
	//取消收藏文件
	function cancelCollectFile(id){
		$.ajax({
			type: 'get',
			url: ' /collect_file/cancel/'+id,
			dataType: 'json',
			success: function (result) {
				if (result.code == 0) {
					$('#iFav'+id).toggleClass("ck");
				}else if(result.code == -1){
					location.href = '/error';
				}
			}
		});
	}
	//删除文件或文件夹
	function deleteFile(id){
		$.ajax({
			type: 'get',
			url: ' /user_file/delete/'+id,
			dataType: 'json',
			success: function (result) {
				if (result.code == 0) {
					location.href = location.href;
				}else if(result.code == -1){
					location.href = '/error';
				}
			}
		});
	}
	//下载文件
	function download(id){
		$.ajax({
			type: 'get',
			url: '/download/auth/'+id,
			data:{is_public:is_public},
			dataType: 'json',
			success: function (result) {
				console.log(result.data.access_token);
				if (result.code == 0) {
					var is_image = $('#iDownLoad'+id).data('is_image');
					if(is_image == '0'){
						var u = navigator.userAgent, app = navigator.appVersion;
						var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
						if(isIOS){
							download_links = "{{ Illuminate\Support\Facades\Request::server ('SERVER_NAME') }}" +'/download/'+ result.data.access_token;
							$('#show-custom').trigger('click');
						}else{
							location.href = '/download/' + result.data.access_token;
						}
					}else{
						location.href = '/download/' + result.data.access_token;
					}
				}else if(result.code == 1){
					$.toast("文件不存在", "cancel");
					setTimeout(function() {
						location.href = location.href;
					}, 1000);
				}else if(result.code == -1){
					location.href = '/error';
				}
			}
		});
	}
	//重命名
	function rename(id, new_name){
		$.ajax({
			type: 'get',
			url: '/user_file/rename/'+id,
			data:{id:id, new_name:new_name},
			dataType: 'json',
			success: function (result) {
				console.log(result);
				if (result.code == 0) {
					$.toast("修改成功");
					$('#iName'+id).html(new_name);
				}else if(result.code == 1){
					$.toast("文件不存在", "cancel");
					setTimeout(function() {
						location.href = location.href;
					}, 1000);
				}else if(result.code == -1){
					location.href = '/error';
				}
			}
		});
	}
	function getFile(){
		var order = $('input[name="order"]').val();
		var key = $('input[name="key"]').val();
		$.ajax({
			type:'get',
			url:' /user_file/folder',
			data:{parent_id:folder_id,is_public:is_public,order:order,key:key},
			dataType:'json',
			success:function(result){console.log(result);
				if(result.code == 0){
					var folder_info = result.data.folder_info;
					if(folder_info.folder.length > 0 || folder_info.file.length > 0){
						$('.pldIcon').hide();
						$('.pInfoBar').hide();
						var html = '';
						if(folder_info.folder.length > 0) {
							$.each(folder_info.folder, function (k, v) {
								html += '<li  class="listItem">'
										+ (is_public == 1 ? '' :
										'<i class="ops"><b></b></i>')
										+ '<a href="/list?folder_id='+ v.id+'&is_public='+is_public+'" class="weui-media-box weui-media-box_appmsg">'
										+ '<div class="weui-media-box__hd">'
										+ '<i class="fileIcon floder"></i>'
										+ '</div>'
										+ '<div class="weui-media-box__bd">'
										+ '<h4 class="weui-media-box__title" id="iName'+ v.id+'">' + v.name + '</h4>'
										+ '<p class="weui-media-box__desc">' + v.updated_at + '</p>'
										+ '</div>'
										+ '</a>'
										+'<div class="opList dis">'
										+ (is_public == 1 ? '' :
										'<a href="javascript:;" class="iDel" id="iDel'+ v.id+'" data-id="'+ v.id+'">'
										+'<div class="btnIcon"><i></i></div>'
										+'<p class="btnLabel">删除</p>'
										+'<a href="javascript:;" class="iEdit" id="iEdit'+ v.id+'" data-id="'+ v.id+'">'
										+'<div class="btnIcon"><i></i></div>'
										+'<p class="btnLabel">重命名</p>'
										+'</a>'
										+'<a href="/list_folder?user_file_id='+ v.id+'&is_public='+is_public+'" class="iMove" id="iMove'+ v.id+'" data-id="'+ v.id+'">'
										+'<div class="btnIcon"><i></i></div>'
										+'<p class="btnLabel">移动</p>'
										+'</a>'
										)
										+'</a>'
										+'</div>'


										+ '</li>';
							});
						}
						if(folder_info.file.length > 0){
							$.each(folder_info.file, function(k, v){
								var file_icon_html = '';
								var is_image = 0;
								if(v.thumb){
									file_icon_html = '<img src="'+ v.thumb +'" alt="" class="weui-media-box__thumb">';
									is_image = 1;
								}else{
									var icon = '';
									icon = getIconClass(v.file_ext.toUpperCase());
									file_icon_html = '<i class="fileIcon '+icon+'"></i>';
								}
								var collectHtml = '<a href="javascript:;" class="iFav" id="iFav'+ v.id+'" data-id="'+ v.id+'">';
								if(v.collect_status == 1){
									collectHtml = '<a href="javascript:;" class="iFav ck" id="iFav'+ v.id+'" data-id="'+ v.id+'">';
								}
								html += '<li  class="listItem">'
										+'<i class="ops"><b></b></i>'
										+'<a href="/detail?user_file_id='+ v.id+'&is_public='+is_public+'" class="weui-media-box weui-media-box_appmsg filetype">'
										+'<div class="weui-media-box__hd">'
										+ file_icon_html
										+'</div>'
										+'<div class="weui-media-box__bd">'
										+'<h4 class="weui-media-box__title" id="iName'+ v.id+'">'+ v.name+'</h4>'
										+'<p class="weui-media-box__desc">'+v.updated_at+'</p>'
										+'</div>'
										+'</a>'
										+'<div class="opList dis">'
										+'<a href="javascript:void(0);" class="iDownload" id="iDownLoad'+ v.id+'" data-id="'+ v.id+'" data-is_image="'+is_image+'">'
										+'<div class="btnIcon"><i></i></div>'
										+'<p class="btnLabel">下载</p>'
										+'</a>'
										+ collectHtml
										+'<div class="btnIcon"><i></i></div>'
										+'<p class="btnLabel">收藏</p>'
										+'</a>'
										+ (is_public == 1 ? '' :
										'<a href="javascript:;" class="iDel" id="iDel'+ v.id+'" data-id="'+ v.id+'">'
										+'<div class="btnIcon"><i></i></div>'
										+'<p class="btnLabel">删除</p>'
										+'</a>'
										+'<a href="javascript:;" class="iEdit" id="iEdit'+ v.id+'" data-id="'+ v.id+'">'
										+'<div class="btnIcon"><i></i></div>'
										+'<p class="btnLabel">重命名</p>'
										+'</a>'
										+'<a href="/list_folder?user_file_id='+ v.id+'&is_public='+is_public+'" class="iMove" id="iMove'+ v.id+'" data-id="'+ v.id+'">'
										+'<div class="btnIcon"><i></i></div>'
										+'<p class="btnLabel">移动</p>'
										+'</a>'
										)
//										+'<a href="/detail?user_file_id='+ v.id+'&is_public='+is_public+'" class="iMore" id="iMore'+ v.id+'" data-id="'+ v.id+'">'
//										+'<div class="btnIcon"><i></i></div>'
//										+'<p class="btnLabel">更多</p>'
										+'</a>'
										+'</div>'
										+'</li>';
							});
						}

						$('.fileList ul').html(html);
					}else{
						$('.pldIcon').show();
						$('.pInfoBar').show();
					}
				}else if(result.code == -1){
					location.href = '/error';
				}
			},
			complete:function () {

			}

		});
	}
	function getIconClass(file_ext){
		switch (file_ext){
			case '.DOC':
			case '.DOCX':
				icon = 'doc';
				break;
			case '.TXT':
				icon = 'txt';
				break;
			case '.ZIP':
			case '.RAR':
				icon = 'zip';
				break;
			case '.PPT':
				icon = 'ppt';
				break;
			case '.XLS':
			case '.XLSX':
				icon = 'excel';
				break;
			default :
				icon = 'common';
		}
		return icon;
	}

	</script>
@stop
